package com.btjava.maven.pomver.handler;

import com.btjava.maven.pomver.utils.XmlParseUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import java.util.List;
import java.util.Objects;

public class PomXmlHandler {
    public static String getArtifactId(String filePath){
        List<Element> elementList = XmlParseUtils.getElementByNamePath(filePath,"artifactId");
        if(Objects.isNull(elementList) || elementList.isEmpty()){
            return "";
        }
        return elementList.get(0).getTextTrim();
    }

    public static void setArtifactId(String filePath,String version){
        Document doc =XmlParseUtils.getDocument(filePath);
        if(Objects.isNull(doc)){
            return;
        }
        List<Element> elementList = XmlParseUtils.getElementByNamePath(doc.getRootElement(),"version");
        if(Objects.isNull(elementList) || elementList.isEmpty()){
            return ;
        }
        Element element = elementList.get(0);
        element.setText(version);
        XmlParseUtils.saveXml(doc,filePath);
    }
}
